package wiki.servlets;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.oreilly.servlet.MultipartRequest;

import wiki.generic.utilities.FormatUnsupportedException;
import wiki.setup.Setup;
/**
 * 
 * @author <p><strong>JwTeam</strong></p> 
 * Responsabile della configurazione delle pagine, come lo stile e il layout
 */
public class ConfigurationServlet extends HttpServlet{

	
	private static final long serialVersionUID = 1L;
	private static final String TITOLO="titolo";
	private static final String DESCR="descrizione";
	private static final String LOGO="logo";
	private static final String STILE="stile";
	private static final String LAY="layout";
	
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession ses=req.getSession(false);
		if(ses!=null){
			boolean isDoc=(Boolean) ses.getAttribute("rights");
				if(isDoc){
					String root="./webapps/wiki/sharing";
					MultipartRequest mul=new MultipartRequest(req, root, 16777216);
					String corso=(String) ses.getAttribute("corso");
					String titolo=mul.getParameter(TITOLO);
					titolo=Setup.getSetup().getMediator().getGpService().changeHTMLText(titolo);
					String descrizione=mul.getParameter(DESCR);
					descrizione=Setup.getSetup().getMediator().getGpService().changeHTMLText(descrizione);
					String stile=mul.getParameter(STILE);
					String layout=mul.getParameter(LAY);	
					changeParameter(corso,TITOLO, titolo);
					changeParameter(corso,DESCR, descrizione);
					changeParameter(corso,STILE, stile);
					changeParameter(corso,LAY, layout);
					
				
					
					File f=mul.getFile(LOGO);
					if(f!=null){
						String fileName=mul.getFilesystemName(LOGO);
						try {
							if(Setup.getSetup().getMediator().getGpService().getFileType(fileName)=="img"){
								
									fileName=fileName.replace("'", "_");
									fileName=fileName.replace("\"", "_");
									fileName=fileName.replace("&", "E");
									fileName=fileName.replace("$$||$$", "$$$$$$");
									String dest=root+"/logo";
									while(Setup.getSetup().getMediator().getGpService().isDuplicate(dest, fileName)){
										fileName=Setup.getSetup().getMediator().getGpService().modifyName(fileName);
									}
									File fOUT=new File(dest,fileName);
									
									FileInputStream fIS=new FileInputStream(f);
									FileOutputStream fOS=new FileOutputStream(fOUT);
									
									while(fIS.available()>0){
										fOS.write(fIS.read());
									}
									fIS.close();
									fOS.close();
									f.delete();
									
									Setup.getSetup().getMediator().getVservice().setParameter(corso,"logo", "./sharing/logo/"+fileName);
								}
								
							
						} catch (FormatUnsupportedException e) {
							resp.sendRedirect("./html/errorpages/nosupport.html");
							return;
						}
					}
					
					resp.sendRedirect("./view?page=index"); 
				} else {
					resp.sendRedirect("./html/errorpages/nopermessi.html");
					return;
				}
		} else{
			resp.sendRedirect("./html/LOGIN/login.html");
			return;
		}
	}
	
	
	private void changeParameter(String corso,String type,String value){
		if(value!=null){
			if(!value.equals("")){
			Setup.getSetup().getMediator().getVservice().setParameter(corso,type, value);
			}
		}
	}
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.sendRedirect("./html/errorpages/nopermessi.html");
		
	}
}
